package com.xgkj.diyiketang.fragment;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RatingBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.xgkj.diyiketang.R;
import com.xgkj.diyiketang.util.PalLog;
import com.xgkj.diyiketang.weidget.NestRadioGroup;

/**
 * A simple {@link Fragment} subclass.
 */
public class Fragment_ChooseDatiType extends BaseFragment implements View.OnClickListener, NestRadioGroup.OnCheckedChangeListener, RatingBar.OnRatingBarChangeListener {

    private DatiFragmentClickListener mListener;

    private RelativeLayout titleLayout;
    private TextView back;
    private NestRadioGroup gradeGroup;
    private RadioButton gaoyi;
    private RadioButton gaoer;
    private RadioButton gaosan;
    private RadioButton chuyi;
    private RadioButton chuer;
    private RadioButton chusan;
    private RadioButton yinianji;
    private RadioButton ernianji;
    private RadioButton sannianji;
    private RadioButton sinianji;
    private RadioButton wunianji;
    private RadioButton liunianji;
    private NestRadioGroup subjectGroup;
    private RadioButton english;
    private RadioButton math;
    private RadioButton chinese;
    private RadioButton all;
    private NestRadioGroup problemGroup;
    private RadioButton xuanze;
    private RadioButton tiankong;
    private NestRadioGroup answerGroup;
    private RadioButton bidati;
    private RadioButton qiangdati;
    private RatingBar jiesuoHuaduo;
    private View view;

    private String mSubjectNo;
    private int rewardCount;
    private String grade;
    private int isopen = 0;
    private String questionType;
    private String answerType;

    @Override
    protected int getContentViewId() {
        return 0;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        if (view == null) {
            view = inflater.inflate(R.layout.fragment_choose_dati_type, null);
        }

        ViewGroup parent = (ViewGroup) view.getParent();
        if (parent != null) {
            parent.removeView(view);
        }
        return view;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        titleLayout = (RelativeLayout) view.findViewById(R.id.title_layout);
        back = (TextView) view.findViewById(R.id.back);
        back.setOnClickListener(this);
        gradeGroup = (NestRadioGroup) view.findViewById(R.id.grade_group);
        gradeGroup.setOnCheckedChangeListener(this);
        gaoyi = (RadioButton) view.findViewById(R.id.gaoyi);
        gaoer = (RadioButton) view.findViewById(R.id.gaoer);
        gaosan = (RadioButton) view.findViewById(R.id.gaosan);
        chuyi = (RadioButton) view.findViewById(R.id.chuyi);
        chuer = (RadioButton) view.findViewById(R.id.chuer);
        chusan = (RadioButton) view.findViewById(R.id.chusan);
        yinianji = (RadioButton) view.findViewById(R.id.yinianji);
        ernianji = (RadioButton) view.findViewById(R.id.ernianji);
        sannianji = (RadioButton) view.findViewById(R.id.sannianji);
        sinianji = (RadioButton) view.findViewById(R.id.sinianji);
        wunianji = (RadioButton) view.findViewById(R.id.wunianji);
        liunianji = (RadioButton) view.findViewById(R.id.liunianji);
        subjectGroup = (NestRadioGroup) view.findViewById(R.id.subject_group);
        subjectGroup.setOnCheckedChangeListener(this);
        english = (RadioButton) view.findViewById(R.id.english);
        math = (RadioButton) view.findViewById(R.id.math);
        chinese = (RadioButton) view.findViewById(R.id.chinese);
        all = (RadioButton) view.findViewById(R.id.all);
        problemGroup = (NestRadioGroup) view.findViewById(R.id.problem_group);
        problemGroup.setOnCheckedChangeListener(this);
        xuanze = (RadioButton) view.findViewById(R.id.xuanze);
        tiankong = (RadioButton) view.findViewById(R.id.tiankong);
        answerGroup = (NestRadioGroup) view.findViewById(R.id.answer_group);
        answerGroup.setOnCheckedChangeListener(this);
        bidati = (RadioButton) view.findViewById(R.id.bidati);
        qiangdati = (RadioButton) view.findViewById(R.id.qiangdati);
        jiesuoHuaduo = (RatingBar) view.findViewById(R.id.jiesuo_huaduo);
        jiesuoHuaduo.setOnRatingBarChangeListener(this);
        view.findViewById(R.id.next_btn).setOnClickListener(this);
    }


    private CheckBox getIsopenCheck() {
        return (CheckBox) getView().findViewById(R.id.isopen_check);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.next_btn:
                if (getIsopenCheck().isChecked()) {
                    isopen = 1;
                }else {
                    isopen = 0;
                }
                mListener.onDatiFragmentClick();
                break;

            case R.id.back:
                if (getActivity() != null) {
                    getActivity().finish();
                }
                break;
        }
    }

    public void setDatiFragmentClickLitener(DatiFragmentClickListener listener) {
        mListener = listener;
    }


    public String getmSubjectNo() {
        return mSubjectNo;
    }

    public int getRewardCount() {
        return rewardCount;
    }

    public String getGrade() {
        return grade;
    }

    public int isopen() {
        return isopen;
    }

    public String getQuestionType() {
        return questionType;
    }

    public String getAnswerType() {
        return answerType;
    }

    private int mCheckId;

    @Override
    public void onCheckedChanged(NestRadioGroup group, int checkedId) {
        if (mCheckId == checkedId) return;
        mCheckId = checkedId;
        switch (checkedId) {
            case R.id.yinianji:
                grade = "1";
                break;
            case R.id.ernianji:
                grade = "2";
                break;
            case R.id.sannianji:
                grade = "3";
                break;
            case R.id.sinianji:
                grade = "4";
                break;

            case R.id.wunianji:
                grade = "5";
                break;
            case R.id.liunianji:
                grade = "6";
                break;
            case R.id.chuyi:
                grade = "7";
                break;
            case R.id.chuer:
                grade = "8";
                break;
            case R.id.chusan:
                grade = "9";
                break;
            case R.id.gaoyi:
                grade = "10";
                break;
            case R.id.gaoer:
                grade = "11";
                break;
            case R.id.gaosan:
                grade = "12";
                break;
            case R.id.english:
                mSubjectNo = "2";
                break;
            case R.id.math:
                mSubjectNo = "1";
                break;
            case R.id.chinese:
                mSubjectNo = "0";
                break;
            case R.id.all:
                mSubjectNo = "3";
                break;
            case R.id.xuanze:
                questionType = "1";
                break;
            case R.id.tiankong:
                questionType = "0";
                break;
            case R.id.bidati:
                answerType = "0";
                break;
            case R.id.qiangdati:
                answerType = "1";
                break;
        }
    }

    @Override
    public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
        PalLog.e("rating 数目:",rating+"");
        rewardCount = (int) rating;
    }


    public interface DatiFragmentClickListener {
        public void onDatiFragmentClick();
    }


}
